package com.tools.common.io;

import com.tools.common.object.Note;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

/**
 * 字符集编码的枚举
 * */
@Note("字符集编码的枚举")
public enum CharsetEnum {

    UTF_8(0,"UTF-8"),
    GBK(1, "GBK"),
    GB18030(2, "GB18030"),
    ASCII(3, "ASCII"),
    ISO_8859_1(4, "ISO-8859-1"),
    UTF_16(5, "UTF-16"),
    UTF_16LE(6, "UTF-16LE"),
    UTF_16BE(7, "UTF-16BE");

    private final int code;
    private final String value;

    CharsetEnum(int code, String value) {
        this.code = code;
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public String getValueLowerCase() {
        return value.toLowerCase(Locale.ROOT);
    }

    public int getCode() {
        return code;
    }

    public boolean eq(String value) {
        return this.value.equalsIgnoreCase(value);
    }

    @Override
    public String toString() {
        return value;
    }

    @Note("获取枚举对象对应的 Charset 字符集实例")
    public Charset toCharset() {
        switch (this) {
            case GBK: return Charset.forName(GBK.value);
            case ASCII: return StandardCharsets.US_ASCII;
            case UTF_16: return StandardCharsets.UTF_16;
            case UTF_16BE: return StandardCharsets.UTF_16BE;
            case UTF_16LE: return StandardCharsets.UTF_16LE;
            case ISO_8859_1: return StandardCharsets.ISO_8859_1;
            case GB18030: return Charset.forName(GB18030.value);
            case UTF_8: return StandardCharsets.UTF_8;
            default: throw new IllegalArgumentException("没有 " + this + " 枚举实例");
        }
    }

    /*
     ****************************************************************************************
     *
     *      静态方法
     *
     * **************************************************************************************
     * */


    @Note("安全地根据字符串名称获取对应的枚举对象，获取不到不会抛异常")
    public static CharsetEnum getEnumSafe(String value) {
        switch (value.toUpperCase(Locale.ROOT)) {
            case "UTF-8": return UTF_8;
            case "GBK": return GBK;
            case "ISO-8859-1": return ISO_8859_1;
            case "UTF-16": return UTF_16;
            case "GB18030": return GB18030;
            case "UTF-16LE": return UTF_16LE;
            case "UTF-16BE": return UTF_16BE;
            case "ASCII": return ASCII;
            default: return null;
        }
    }


    @Note("根据字符串名称获取对应的枚举对象，获取不到会抛异常")
    public static CharsetEnum getEnum(String value) {
        CharsetEnum charset = getEnumSafe(value);
        if(charset == null) throw new IllegalArgumentException("不知道 " + value + " 是什么字符集编码");
        return charset;
    }


    @Note("安全地根据 code 编码获取对应的枚举对象，获取不到不会抛异常")
    public static CharsetEnum getEnumSafe(int code) {
        switch (code) {
            case 0: return UTF_8;
            case 1: return GBK;
            case 2: return GB18030;
            case 3: return ASCII;
            case 4: return ISO_8859_1;
            case 5: return UTF_16;
            case 6: return UTF_16LE;
            case 7: return UTF_16BE;
            default: return null;
        }
    }


    @Note("根据 code 编码获取对应的枚举对象，获取不到会抛异常")
    public static CharsetEnum getEnum(int code) {
        CharsetEnum charset = getEnumSafe(code);
        if(charset == null) throw new IllegalArgumentException("不知道 " + code + " 代表什么字符集编码");
        return charset;
    }
}
